/**
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
*
* BaseCurrencyCheckItemInsService
* 通用检查项实例
*
* 修改纪录
* 2021-07-07 版本：1.0 chengengwei 创建。
* @version 版本：1.0
* @author 作者：chengengwei
* 创建日期：2021-07-07
*/
package com.topscomm.base.service.impl;

import com.topscomm.base.pojo.BaseCurrencyCheckItemEntity;
import com.topscomm.base.pojo.BaseCurrencyCheckItemInsEntity;
import com.topscomm.base.service.IBaseCurrencyCheckItemGroupService;
import com.topscomm.base.service.IBaseCurrencyCheckItemService;
import com.topscomm.basic.BasicEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.base.service.IBaseCurrencyCheckItemInsService;
import com.topscomm.base.service.impl.auto.BaseCurrencyCheckItemInsServiceAuto;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service(value = "baseCurrencyCheckItemInsService")
@Scope(value = "singleton")
public class BaseCurrencyCheckItemInsService extends BaseCurrencyCheckItemInsServiceAuto implements IBaseCurrencyCheckItemInsService {

    @Autowired
    private IBaseCurrencyCheckItemService baseCurrencyCheckItemService;

    @Autowired
    private IBaseCurrencyCheckItemGroupService baseCurrencyCheckItemGroupService;

    /**
     * @description:查询检查项实例列表
     * @param paraMap
     * @return
     * @author: chengengwei
     * @date: 2021-07-07
     * @modify:
     */
    @Override
    public List<Map<String, Object>> queryList(Map<String, Object> paraMap) {
        String sourcetype = ConvertUtil.convertToString(paraMap.get("sourcetype"));
        String sourceid = ConvertUtil.convertToString(paraMap.get("sourceid"));
        String relationcode = ConvertUtil.convertToString(paraMap.get("relationcode"));
        List<Map<String, Object>> resultList = new ArrayList<>();
        StringBufferProxy whereSql = new StringBufferProxy();
        // 判断检查项组是否有效
        whereSql.appendSingle(" code = '{0}' and enabled = 1", relationcode);
        int count = this.baseCurrencyCheckItemGroupService.getCount(whereSql.toString());
        if (count < 1) {
            return resultList;
        }
        whereSql.clear();
        // 查询检查项表中有效的相关记录
        whereSql.appendSingle(" {0} = '{1}' and enabled = 1 ", BaseCurrencyCheckItemEntity.FieldRelationcode,
                relationcode);
        List<BaseCurrencyCheckItemEntity> checkItemList = this.baseCurrencyCheckItemService
                .queryByWhere(whereSql.toString());
        whereSql.clear();
        // 查询检查项实例表中相关记录
        whereSql.appendSingle(" {0} = '{1}' and {2} = '{3}' and {4} = '{5}' ",
                BaseCurrencyCheckItemInsEntity.FieldSourcetype, sourcetype,
                BaseCurrencyCheckItemInsEntity.FieldSourceid, sourceid,
                BaseCurrencyCheckItemInsEntity.FieldRelationcode, relationcode);
        List<BaseCurrencyCheckItemInsEntity> checkItemInsList = this.queryByWhere(whereSql.toString());
        Map<String, List<BaseCurrencyCheckItemInsEntity>> checkItemInsMapList = checkItemInsList.stream()
                .collect(Collectors.groupingBy(BaseCurrencyCheckItemInsEntity::getCheckcode));
        // 若checkItemList在checkItemInsList中对应的数据存在，则用后者数据代替前者
        checkItemList.forEach(item -> {
            Map<String, Object> tempMap = new HashMap<>();
            tempMap.put(BaseCurrencyCheckItemInsEntity.FieldCheckcode, item.getCode());
            tempMap.put(BaseCurrencyCheckItemInsEntity.FieldCheckname, item.getName());
            tempMap.put(BaseCurrencyCheckItemInsEntity.FieldIntroduce, item.getIntroduce());
            tempMap.put(BaseCurrencyCheckItemInsEntity.FieldRelationcode, item.getRelationcode());
            List<BaseCurrencyCheckItemInsEntity> tempCheckItemInsList = checkItemInsMapList.get(item.getCode());
            if (tempCheckItemInsList != null && tempCheckItemInsList.size() > 0) {
                BaseCurrencyCheckItemInsEntity tempCheckItemInsEntity = tempCheckItemInsList.get(0);
                tempMap.put(BaseCurrencyCheckItemInsEntity.FieldIntroduce, tempCheckItemInsEntity.getIntroduce());
                tempMap.put(BaseCurrencyCheckItemInsEntity.FieldResult, tempCheckItemInsEntity.getResult());
                tempMap.put(BaseCurrencyCheckItemInsEntity.FieldDescription, tempCheckItemInsEntity.getDescription());
                tempMap.put(BaseCurrencyCheckItemInsEntity.FieldSourcetype, tempCheckItemInsEntity.getSourcetype());
                tempMap.put(BaseCurrencyCheckItemInsEntity.FieldSourceid, tempCheckItemInsEntity.getSourceid());
                // id非0则说明该条检查项已填写并暂存过，便于区分新增还是更新。
                tempMap.put("id", tempCheckItemInsEntity.getId());
            } else {
                tempMap.put("id", 0);
            }
            resultList.add(tempMap);
        });
        return resultList;
    }

    /**
     * @description: 暂存检查项实例
     * @author: chengengwei
     * @date: 2021-07-08
     * @modify:
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Map<String, Object> paraMap) {
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) paraMap.get("datas");
        String sourcetype = ConvertUtil.convertToString(paraMap.get(BaseCurrencyCheckItemInsEntity.FieldSourcetype));
        Long sourceid = ConvertUtil.convertToLong(paraMap.get(BaseCurrencyCheckItemInsEntity.FieldSourceid));
        String relationcode = ConvertUtil.convertToString(paraMap.get("relationcode"));
        CboUserEntity user = getSessionUserBean().getUser();
        // 先删除历史数据
        StringBufferProxy whereSql = new StringBufferProxy();
        whereSql.appendSingle(" {0} = '{1}' and {2} = '{3}' and {4} = '{5}' ",
                BaseCurrencyCheckItemInsEntity.FieldSourcetype, sourcetype,
                BaseCurrencyCheckItemInsEntity.FieldSourceid, sourceid,
                BaseCurrencyCheckItemInsEntity.FieldRelationcode, relationcode);
        this.deleteByWhere(whereSql.toString());
        // 新增数据
        List<BasicEntity> insertBatchList = new ArrayList<>();
        dataList.forEach(item -> {
            // 未填写检查结果的不作为新增数据
            if (item.containsKey(BaseCurrencyCheckItemInsEntity.FieldResult)) {
                item.put(BaseCurrencyCheckItemInsEntity.FieldHandleusercode, user.getCode());
                item.put(BaseCurrencyCheckItemInsEntity.FieldHandleusername, user.getName());
                item.put(BaseCurrencyCheckItemInsEntity.FieldHandletime, new Date());
                item.put(BaseCurrencyCheckItemInsEntity.FieldSourcetype, sourcetype);
                item.put(BaseCurrencyCheckItemInsEntity.FieldSourceid, sourceid);
                BaseCurrencyCheckItemInsEntity tempEntity = new BaseCurrencyCheckItemInsEntity();
                tempEntity.convertFromMap(item);
                insertBatchList.add(tempEntity);
            }
        });
        this.insertBatch(insertBatchList);
    }
}
